<template>
  <el-dialog
    :visible="visible"
    :center="true"
    width="460px"
    :before-close="handleClose"
  >
    <span slot="title">{{ type == 'edit' ? '修改用户' : '添加用户' }}</span>
    <el-form
      :model="ruleForm"
      :rules="rules"
      :label-position="labelPosition"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="用户名称" prop="username">
        <el-input v-model="ruleForm.username"></el-input>
      </el-form-item>
      <el-form-item label="时间" prop="create_time">
        <el-date-picker
          type="date"
          placeholder="选择日期"
          v-model="ruleForm.create_time"
          style="width: 100%"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="价格" prop="goods_price">
        <el-select v-model="ruleForm.goods_price" placeholder="请选择活动区域">
          <el-option label="区域一" value="10000"></el-option>
          <el-option label="区域二" value="15000"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >保存</el-button
        >
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
export default {
  name: 'Dialog',
  props: { visible: Boolean, type: String, userInfo: Object },
  data() {
    return {
      ruleForm: Object.assign({}, this.userInfo),
      labelPosition: 'right',
      rules: {
        username: [
          { required: true, message: '请输入用户名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    visible: {
      immediate: true,
      handler(val) {
        val
          ? (this.ruleForm = Object.assign(this.userInfo))
          : (this.ruleForm = {})
      }
    }
  },
  methods: {
    // 关闭弹窗
    handleClose() {
      this.updateVisible(false)
    },
    // 提交
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          console.log(this.type)
          if (this.type == 'edit') {
            this.EditUser()
          } else {
            console.log(111)
            this.addUser()
          }
        } else {
          return false
        }
      })
    },
    // 添加用户
    async addUser() {
      var arr = JSON.parse(localStorage.getItem('Info')) ?? []
      arr.push({ id: Math.ceil(Math.random() * 10), ...this.ruleForm })
      localStorage.setItem('Info', JSON.stringify(arr))
      this.$message.success(this.$t('message.updeteSuccess'))
      this.updateVisible(false)
      this.$emit('done')
    },
    // 编辑用户
    async EditUser() {
      var arr = JSON.parse(localStorage.getItem('Info'))
      const _arr = arr.map((el) => {
        return el.id === this.ruleForm.id ? (el = this.ruleForm) : el
      })
      localStorage.setItem('Info', JSON.stringify(_arr))
      this.$message.success(this.$t('message.updeteSuccess'))
      this.updateVisible(false)
      this.$emit('done')
    },
    // 重置表格
    resetForm(formName) {
      this.ruleForm = {}
      this.$refs[formName].resetFields()
    },
    /* 更新visible */
    updateVisible(value) {
      this.$refs['ruleForm'].resetFields()

      this.$emit('update:visible', value)
    }
  }
}
</script>

<style lang="scss" scoped></style>
